package com.eldenring._02_xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {

    public static void main(String[] args) throws Exception {

        // 1.创建生产者
        DefaultMQProducer producer = new DefaultMQProducer("xhr_group_producer");
        // 2.创建连接  127.0.0.1:9876 nameServer的url地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 3.启动生产者
        producer.start();
        // 4.发送异步消息
        Message message = new Message("xhr", "group", "异步消息测试".getBytes(StandardCharsets.UTF_8));
        // 5.发送消息
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult + "发送消息成功");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("发送消息失败");
            }
        });
    }
}
